package project.server.handlers;

import java.util.ArrayList;
import java.util.Iterator;

import project.server.util.Logger;
import project.server.handlers.Client;

/**
 * Kasutatakse et hoiustada klientide sessioone.
 * @author Rauno Veberson
 *
 */
public class Sessions {
	
	/** The session list. */
	private ArrayList<Client> clientList = new ArrayList<Client>();
	
	/** The logger to use for error reporting. */
	private Logger logger;
	
	/**
	 * Loob uue Sessions objekti.
	 * 
	 * @param logger
	 *            the logger
	 */
	public Sessions(Logger logger) {
		this.logger = logger;
	}
	
	/**
	 * Lisab sessiooni clientList-i.
	 * 
	 * @param cli
	 *            sessioon mida üritatakse lisada
	 */
	public synchronized void addSession(Client cli) {
		clientList.add(cli);
		logger.log("New client connected: " + cli);
	}
	
	/**
	 * Iterator - tagastab clientList-i iteraatori.
	 * 
	 * @return the iterator
	 */
	public synchronized Iterator<Client> iterator() {
		return clientList.iterator();
	}
	
	/**
	 * Eemaldab sessiooni listist.
	 * 
	 * @param cli
	 *            the client to be removed from the list
	 */
	public synchronized void removeFromList(Client cli) {
		clientList.remove(cli);
	}
}
